

***Prepare data to be merged with RAND file***

do 1a_cognitive.do //it adds variables on cognitive abilities (HRS Imputation of Cognitive Functioning Measures)
do 1b_numeracy.do //it adds variables on numeracy tests
do 1c_wellbeing.do //it adds variables on life satisfaction, optimism, financial strain, and well-being

***RAND data***

use "${original}rndhrs_p", clear

*Add data cognitive ability
merge 1:1 hhid pn using cog2014org.dta
rename _merge _merge0
drop if _merge0==0

*Add data about numeracy scores
merge 1:1 hhid pn using numeracy.dta
rename _merge _merge1
drop if _merge1==2

*Add data wellbeing
merge 1:1 hhid pn using wellbeing.dta
rename _merge _merge2
drop if _merge2 == 2

*Add data about polygenic scores - European Ancestry
merge 1:1 hhid pn using "${original}pgenscore4e_r"
rename _merge _merge3
rename PC* E4_PC*


*** --- copy and clean variable labels --- ***
foreach v of var * { 
	local l`v' : variable label `v'
	local l0`v': subinstr local l`v' "`v':" "",all
	local l1`v': subinstr local l0`v' "`v': " "",all

	foreach i of numlist 12/1 {
	local l2`v': subinstr local l1`v' "w`i' " "",all
	local l3`v': subinstr local l2`v' " s " "",w
	local l4`v': subinstr local l3`v' " r " "",w
	}
}
*** --- rename variables to accomodate reshape --- ***
foreach i of numlist 12/1 {
ren s`i'* *__s__`i'
ren r`i'* *__r__`i'
ren h`i'* *__h__`i'
} 

ren *__12 *2014
ren *__11 *2012
ren *__10 *2010
ren *__9 *2008
ren *__8 *2006
ren *__7 *2004
ren *__6 *2002
ren *__5 *2000
ren *__4 *1998
ren *__3 *1996
ren *__2 *1994
ren *__1 *1992

*** --- store unique stub names throughout all waves in macro to accomodate reshape --- ***
unab vars: *2014 *2012 *2010 *2008 *2006 *2004 *2002 *2000 *1998 *1996 *1994 *1992 
local stub1: subinstr local vars "2014" "",all
local stub2: subinstr local stub1 "2012" "",all
local stub3: subinstr local stub2 "2010" "",all
local stub4: subinstr local stub3 "2008" "",all
local stub5: subinstr local stub4 "2006" "",all
local stub6: subinstr local stub5 "2004" "",all
local stub7: subinstr local stub6 "2002" "",all
local stub8: subinstr local stub7 "2000" "",all
local stub9: subinstr local stub8 "1998" "",all
local stub10: subinstr local stub9 "1996" "",all
local stub11: subinstr local stub10 "1994" "",all
local stub12: subinstr local stub11 "1992" "",all
local uniquestubs: list uniq stub12 //
compress

*** --- perform the actual reshape --- ***
reshape long `uniquestubs' , i(hhidpn) j(year)

*** --- store variable names in macro and clean macro for relabeling --- ***
unab allvars__s: *__s 
unab allvars__r: *__r
unab allvars__h: *__h


local allvars_s: subinstr local allvars__s "__s" "",all
local allvars_r: subinstr local allvars__r "__r" "",all
local allvars_h: subinstr local allvars__h "__h" "",all

*** --- relabel variables that lost label in reshape --- ***
foreach n in s r h {
	foreach v of local allvars_`n' { 
		foreach y of numlist 1/12 {
				local L`v'__`n' : variable label `v'__`n' 
				if `"`L`v'__`n''"' == "" & `"`l4`n'`y'`v''"' != "" { 
					label var `v'__`n' `"`l4`n'`y'`v''"' 
				}
			}
	}
}

*** --- attach cleaned label to variables unaffected by reshape --- ***
unab allvars: _all
local restvars__r: list allvars - allvars__s
local restvars__h: list restvars__r - allvars__r
local restvars: list restvars__h - allvars__h

foreach v of local restvars {
label var `v' `"`l1`v''"' 
}

*** --- rename variables in more convenient way --- ***
ren *__s s_*
ren *__r *
ren *__h h_*

compress

save rndhrs_p_long,replace

use rndhrs_p_long,clear

*** --- sample selection --- ***
global id "hhidpn hhid pn year raovrlap inw1-inw12  iwstat hacohort  rawtsamp raehsamp raestrat wthh  wtresp h_anyfin finr   proxy s_ddate"
global chr "agem_b  agem_e  agem_m   ragender  raracem  cendiv  cenreg mstat   mstath  mstatf mpart h_hhres h_child s_evbrn raevbrn raddate"
global sec " raedyrs rassrecv sassrecv radnepi  unemp lbrf inlbrf"
global hlt "diab* canc* heart* cond* strok* hosp  shlt  shltc  shltcf money moneyr moneya  adla iadla   cesd cesdm eata  walkra  beda  batha  dressa  phonea  moneya  medsa "
global cog "memryq memry memrye memrys memryf  slfmem  pstmem imrc  imrc20   aimr10 himr20  dlrc   adlr10 hdlr20  dlrc20  ser7  bwc20  bwc86 mo dy yr dw  scis  cact   pres vp  vocab htr40 s_tr40 tr40  tr20  atr20  amstot  acgtot  mstot  cogtot nsscre nsscse  num1_disease num2_lottery num3_interest"
global hbh " bmi  ltactf vgactf    drink drinkd drinkn  smokev smoken"
global cfl " status  flag notics  novoc  fslfme  fpstme  fimrc  fdlrc  fser7  fbwc20  fbwc86     fmo fdy fyr fdw  fscis  fcact   fpres fvp fvocab"
global fin "h_arles h_atran h_absns h_aira h_astck h_achck h_acd h_abond h_aothr h_adebt h_ahous h_amort h_ahmln h_atoth h_ahoub h_amrtb h_anethb h_atotf h_atota h_atotb h_atotw h_atotn h_atotac h_atotbc h_atotwc h_atothc h_atotfc h_atotnc"
global ffl "h_afrles h_aftran h_afbsns h_afira h_afstck h_afchck h_afcd h_afbond h_afothr h_afdebt h_afhous h_afmort h_afhmln h_afhoub h_afmrtb "
global livprob "liv75 liv75p liv75r liv75c liv75f liv10a liv10 liv10p liv10r liv8xc liv8xf liv85 liv85p liv85r liv85c liv85f"
global oop "s_oopmdof oopmd oopmdf oopmdo oopmdof h_oopmd h_oopma h_oopmdf h_oopmaf"
global medexp "govmr govmd  partd  totmbf totmbi totmb"
global date "s_iwbeg iwbeg s_iwend s_iwendf s_iwendm s_iwendy iwend iwendf iwendm iwendy"
global mood "lifesat_c optimism_c contr_fin stress_e"
global pgs_ea "E4_PC* E4_GCOG2_CHARGE18 E4_EDU3_SSGAC18 E4_GWAD2NA_IGAP19"
global health_beh "drink  drinkd  drinkn  smokev  smoken  vigact vgactx  mdactx  ltactx  cholst  flusht  breast  mammog  papsm  prost  dentst"
global heins "henum  lifein  hiltc s_hiltc tyltc"

keep $id $chr $sec $hlt $cog  $hbh $cfl $fin $ffl $livprob $oop $medexp $heins $date  $mood $pgs_ea $health_beh

*** --- total assets incl 2nd home, excl IRAs, and non-housing wealth --- ***
ren h_af* f_h_a*
local dtotb "h_amort h_ahmln h_adebt h_amrtb"
local atotb "h_ahous h_ahoub h_arles h_atran h_absns h_aira h_astck h_achck h_acd h_abond h_aothr"
local dtotf "h_adebt"
local atotf "h_astck h_achck h_acd h_abond h_aothr"
egen atotb = rowtotal(`atotb') if h_atotb ~= . & h_atotb ~= .q
egen dtotb = rowtotal(`dtotb') if h_atotb ~= . & h_atotb ~= .q
egen atotf = rowtotal(`atotf') if h_atotf ~= . & h_atotf ~= .q
egen dtotf = rowtotal(`dtotf') if h_atotf ~= . & h_atotf ~= .q

*** --- Asset imputation level including 2nd home --- ***
foreach a of varlist h_ahous h_ahoub h_arles h_atran h_absns h_astck h_achck h_acd h_abond h_aothr {
	g b_impw_`a' = (`a' / atotb ) if f_`a' > 2 & f_`a' ~=6 & h_atotb ~= . & h_atotb ~= .q
}
foreach d of varlist h_adebt h_amrtb {
	g b_impw_`d' = (`d' / dtotb ) if f_`d' > 2 & f_`d' ~=6 & h_atotb ~= . & h_atotb ~= .q
}
foreach d of varlist h_amort h_ahmln {
	g b_impw_`d' = (`d' / dtotb ) if f_`d' > 1 & h_atotb ~= . & h_atotb ~= .q
}
g b_impw_aira = (h_aira / atotb ) if f_h_aira > 1 & h_atotb ~= . & h_atotb ~= .q

***--- Asset imputation level Net value of non-housing financial wealth ---***
foreach a of varlist h_astck h_achck h_acd h_abond h_aothr {
	g f_impw_`a' = (`a' / atotf ) if f_`a' > 2 & f_`a' ~=6 & h_atotf ~= . & h_atotf ~= .q
}
g f_impw_h_adebt = (h_adebt / dtotf ) if f_h_adebt > 2 & f_h_adebt ~=6 & h_atotf ~= . & h_atotf ~= .q

*** --- calculate final imputation level ---***
ren *impw_h* *impw*
egen impw_atotb = rowtotal(b_impw*)
egen impw_atotf = rowtotal(f_impw*)

drop b_impw* f_impw*

foreach i of varlist impw_atot* {
g ad_`i'= `i'/2
}

*** --- generate imputation flags --- ***
g compl_totb=((f_h_ahous==1 | f_h_ahous==6) & (f_h_ahoub==1 | f_h_ahoub==6) & (f_h_arles==1 | f_h_arles==6) & (f_h_atran==1 | f_h_atran==6 ) & ( f_h_absns==1 | f_h_absns==6 ) & ( f_h_aira==1 | f_h_aira==0 ) & ( f_h_astck==1 | f_h_astck==6 ) & ( f_h_achck==1 | f_h_achck==6 ) & ( f_h_acd==1 | f_h_acd==6 ) & ( f_h_abond==1 | f_h_abond==6 ) & ( f_h_aothr==1 | f_h_aothr==6 ) & ( f_h_amort==1 | f_h_amort==0 ) & ( f_h_ahmln==1 | f_h_ahmln==0 ) & ( f_h_adebt==1 | f_h_adebt==6 ) & ( f_h_amrtb==1 | f_h_amrtb==6) )
g brack_totb=((f_h_ahous<3 | f_h_ahous==6) & (f_h_ahoub<3 | f_h_ahoub==6) & (f_h_arles<3 | f_h_arles==6) & (f_h_atran<3 | f_h_atran==6) & (f_h_absns<3 | f_h_absns==6) & (f_h_aira==1 | f_h_aira==0) & (f_h_astck<3 | f_h_astck==6) & (f_h_achck<3 | f_h_achck==6) & (f_h_acd<3 | f_h_acd==6) & (f_h_abond<3 | f_h_abond==6) & (f_h_aothr<3 | f_h_aothr==6) & (f_h_amort==1 | f_h_amort==0) & (f_h_ahmln==1 | f_h_ahmln==0) & (f_h_adebt<3 | f_h_adebt==6) & (f_h_amrtb<3 | f_h_amrtb==6) )
g ptimp_totb=((f_h_ahous~=9 & f_h_ahous~=7 & f_h_ahous~=. ) & ( f_h_ahoub~=9 & f_h_ahoub~=7 & f_h_ahoub~=. ) & ( f_h_arles~=9 & f_h_arles~=7 & f_h_arles~=. ) & ( f_h_atran~=9 & f_h_atran~=7 & f_h_atran~=. ) & ( f_h_absns~=9 & f_h_absns~=7 & f_h_absns~=. ) & ( f_h_aira~=9 & f_h_aira~=. ) & ( f_h_astck~=9 & f_h_astck~=7 & f_h_astck~=. ) & ( f_h_achck~=9 & f_h_achck~=7 & f_h_achck~=. ) & ( f_h_acd~=9 & f_h_acd~=7 & f_h_acd~=. ) & ( f_h_abond~=9 & f_h_abond~=7 & f_h_abond~=. ) & ( f_h_aothr~=9 & f_h_aothr~=7 & f_h_aothr~=. ) & ( f_h_amort~=9 & f_h_amort~=. ) & ( f_h_ahmln~=9 & f_h_ahmln~=. ) & ( f_h_adebt~=9 & f_h_adebt~=7 & f_h_adebt~=. ) & ( f_h_amrtb~=9 & f_h_amrtb~=7 & f_h_amrtb~=.))
g ttimp_totb=(f_h_ahous~=. & f_h_ahoub~=. & f_h_arles~=. & f_h_atran~=. & f_h_absns~=. & f_h_aira~=. & f_h_astck~=. & f_h_achck~=. & f_h_acd~=. & f_h_abond~=. & f_h_aothr~=. & f_h_amort~=. & f_h_ahmln~=. & f_h_adebt~=. & f_h_amrtb~=.)

g compl_totf=(( f_h_astck==1 | f_h_astck==6 ) & ( f_h_achck==1 | f_h_achck==6 ) & ( f_h_acd==1 | f_h_acd==6 ) & ( f_h_abond==1 | f_h_abond==6 ) & ( f_h_aothr==1 | f_h_aothr==6 ) & ( f_h_adebt==1 | f_h_adebt==6 ))
g brack_totf=(( f_h_astck<3 | f_h_astck==6 ) & ( f_h_achck<3 | f_h_achck==6 ) & ( f_h_acd<3 | f_h_acd==6 ) & ( f_h_abond<3 | f_h_abond==6 ) & ( f_h_aothr<3 | f_h_aothr==6 ) & ( f_h_adebt<3 | f_h_adebt==6) )
g ptimp_totf=(f_h_astck~=9 & f_h_astck~=7 & f_h_astck~=. & f_h_achck~=9 & f_h_achck~=7 & f_h_achck~=. & f_h_acd~=9 & f_h_acd~=7 & f_h_acd~=. & f_h_abond~=9 & f_h_abond~=7 & f_h_abond~=. & f_h_aothr~=9 & f_h_aothr~=7 & f_h_aothr~=. & f_h_adebt~=9 & f_h_adebt~=7 & f_h_adebt~=.)
g ttimp_totf=(f_h_astck~=. & f_h_achck~=. & f_h_acd~=. & f_h_abond~=. & f_h_aothr~=. & f_h_adebt~=.)

*** --- gen interview dates --- ***
g iwbegm = month(iwbeg)
g iwbegy = year(iwbeg)
lab var iwbegm "interview begin month"
lab var iwbegy "interview begin year"

*** --- generate expected values --- ***

do 1d_thomreut_returns.do

clonevar month = iwbegm
clonevar h_achck2 = h_achck
cap drop _merge
merge m:1 year month using ret_assets,keep(1 3) nogen
bys hhidpn (year): g ygap = iwbegy[_n+1] - iwbegy if iwbegy[_n+1] > iwbegy & iwbegy!=. & iwbegy[_n+1] != .
bys hhidpn (year): g mgap = iwbegm[_n+1] - iwbegm
g timegap = 12*ygap+mgap
***
local assets "bond stck cd debt chck chck2"
foreach a of local assets {
	bys hhidpn (year) : g evf_`a' = h_a`a'[_n-1] 
		forval n = 1/48 {
			bys hhidpn (year) : replace evf_`a' = evf_`a' * (1+ret_`a'_tp`n'[_n-1]/12) if `n'<= timegap
		}
			lab var evf_`a' "Expected Value `a's at t-2"
}
g evf_totf = evf_bond + evf_stck + evf_cd + evf_chck + h_aothr - evf_debt
lab var evf_totf "Expected Value Tot Financial Wealth"

*** --- label variables --- ***
lab var atotb "Assets - Wealth incl 2nd Home"
lab var atotf "Assets - Non-housing financial wealth"
lab var dtotb "Liabilities - Wealth incl 2nd Home"
lab var dtotf "Liabilities - Non-housing financial wealth"
lab var impw_atotb "Imputation weight - Wealth incl 2nd Home"
lab var impw_atotf "imputation weight - Non-housing financial wealth"
lab var ad_impw_atotb "Adj Imp weight - Wealth incl 2nd Home"
lab var ad_impw_atotf "Adj Imp weight - Non-housing financial wealth"
lab var compl_totb "Flag: Only continuous values - Wealth incl 2nd Home"
lab var compl_totf "Flag: Only continuous values - Non-housing financial wealth"
lab var brack_totb "Flag: Continuous values and complete brackets - Wealth incl 2nd Home"
lab var brack_totf "Flag: Continuous values and complete brackets - Non-housing financial wealth"
lab var ptimp_totb "Flag: Cont val,(in)comp brackets,no value - Wealth incl 2nd Home"
lab var ptimp_totf "Flag: Cont val,(in)comp brackets,no value - Non-housing financial wealth"
lab var ttimp_totb "Flag: Cont val,(in)comp brackets,no val,no response - Wealth incl 2nd Home"
lab var ttimp_totf "Flag: Cont val,(in)comp brackets,no val,no response - Non-housing financial wealth"

order hhidpn year hhid pn
save sample,replace

*Erase temporary datasets
erase cog2014org.dta
erase numeracy.dta
erase wellbeing.dta
erase ret_assets.dta
erase rndhrs_p_long.dta

*Add other variables:
do 1e_tracker //tracker file needed to merge with other HRS modules
do 1f_transfers //it adds variables on transfers to children
do 1g_camsrand   //it adds RAND-CAMS consumption variables
do 1h_capgain  //it adds HRS-Core variables needed to contruct the finacial activity variable
do 1i_assetverif  //it adds assets verification variables

save full_data.dta, replace
compress
